home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / thandlr.exe / TDEMO.CPP < prev    next >
C/C++ Source or Header  |  1993-02-21  |  15KB  |  607 lines

  1. /*-----------------------------------------------------------------------*/
  2. /* filename -       tdemo.cpp                                            */
  3. /*                                                                       */
  4. /* function(s)                                                           */
  5. /*                  test module for the THandler class                   */
  6. /*                                                                       */
  7. /*                                                                       */
  8. /* author -         Michael Newton                                       */
  9. /*-----------------------------------------------------------------------*/
  10.  
  11. /*-----------------------------------------------------------------------*/
  12. /*                                                                       */
  13. /*    TDEMO.CPP                                                          */
  14. /*                                                                       */
  15. /*    THandler - Version 1.01                                            */
  16. /*    Copyright (C) 1992, 1993 Michael Newton                            */
  17. /*    All Rights Reserved.                                               */
  18. /*                                                                       */
  19. /*-----------------------------------------------------------------------*/
  20.  
  21.  
  22. // Define Turbo Vision classes used ***************************************
  23.  
  24. #define Uses_TApplication
  25. #define Uses_TButton
  26. #define Uses_TCheckBoxes
  27. #define Uses_TDeskTop
  28. #define Uses_TDialog
  29. #define Uses_TDisplay
  30. #define Uses_TEvent
  31. #define Uses_TGroup
  32. #define Uses_TKeys
  33. #define Uses_TMenu
  34. #define Uses_TMenuBar
  35. #define Uses_TMenuItem
  36. #define Uses_TObject
  37. #define Uses_TPalette
  38. #define Uses_TProgram
  39. #define Uses_TRect
  40. #define Uses_TScreen
  41. #define Uses_TStatusDef
  42. #define Uses_TStatusItem
  43. #define Uses_TStatusLine
  44. #define Uses_TStaticText
  45. #define Uses_TSItem
  46. #define Uses_TSubMenu
  47. #define Uses_TView
  48. #define Uses_TWindow
  49. #define Uses_MsgBox
  50. #define Uses_fpstream
  51. #define Uses_ipstream
  52. #define Uses_opstream
  53.  
  54. #include <tv.h>
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. // CRTL header files ******************************************************
  62.  
  63. #if !defined __STDLIB_H
  64. #include <stdlib.h>
  65. #endif
  66.  
  67. #if !defined __STDIO_H
  68. #include <stdio.h>
  69. #endif
  70.  
  71. #if !defined __STRING_H
  72. #include <string.h>
  73. #endif
  74.  
  75. #if !defined __STRSTREAM_H
  76. #include <strstrea.h>
  77. #endif
  78.  
  79. #if !defined __FSTREAM_H
  80. #include <fstream.h>
  81. #endif
  82.  
  83. #if !defined __IOMANIP_H
  84. #include <iomanip.h>
  85. #endif
  86.  
  87. #if !defined __DOS_H
  88. #include <dos.h>
  89. #endif
  90.  
  91. #if !defined __DIR_H
  92. #include <dir.h>
  93. #endif
  94.  
  95. #if !defined __ALLOC_H
  96. #include <alloc.h>
  97. #endif
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. // Non-CRTL header files **************************************************
  106.  
  107. #if !defined __THANDLER_HPP
  108. #include "thandler.hpp"
  109. #endif
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117. // Command constants ******************************************************
  118.  
  119. const cmTests  = 1000,
  120.       cmDriveA = 1001,
  121.       cmDriveB = 1002,
  122.       cmLpt1   = 1003,
  123.       cmAux    = 1004,
  124.       cmToggle = 1005,
  125.       cmAbout  = 1006,
  126.       cmMode   = 1007,
  127.       hcNone   = hcNoContext;
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. // Non-member function declarations ***************************************
  136.  
  137. void exitfunc(void);
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144. // TMenuItem **************************************************************
  145.  
  146. TMenuItem& operator +(TMenuItem& item1, TMenuItem& item2)
  147. {
  148.    TMenuItem *p = &item1;
  149.    while(p->next != NULL)
  150.       p = p->next;
  151.    p->next = &item2;
  152.    return item1;
  153. }
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160. // TDemoDialog ************************************************************
  161.  
  162. class TDemoDialog : public TDialog
  163. {
  164.    public:
  165.       TDemoDialog(const TRect& bounds, const char *aTitle) :
  166.          TDialog(bounds, aTitle),
  167.          TWindowInit(TDemoDialog::initFrame){};
  168.       virtual void handleEvent(TEvent& event);
  169. };
  170.  
  171.  
  172. void TDemoDialog::handleEvent(TEvent& event)
  173. {
  174.    TDialog::handleEvent(event);
  175.  
  176.    switch(event.what)
  177.       {
  178.       case evCommand:
  179.          switch(event.message.command)
  180.             {
  181.             case cmDriveA:        // Our commands
  182.             case cmDriveB:
  183.             case cmLpt1:
  184.             case cmAux:
  185.             if((state & sfModal) != 0)
  186.                {
  187.                endModal(event.message.command);
  188.                clearEvent(event);
  189.                }
  190.             break;
  191.             }
  192.       break;
  193.       }
  194. }
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203. // THandlerDemo ***********************************************************
  204.  
  205. // Color palette
  206. #define cpC \
  207.    "\x09\x17\x18\x1F\x0E\x04\x0F\x10\x17\x0E" \
  208.    "\x09\x1A\x17\x1C\x1E\x37\x3F\x0E\x0C\x13" \
  209.    "\x30\x3C\x00\x78\x70\x0E\x0E\x13\x70\x40" \
  210.    "\x1E\x78\x70\x0E\x6E\x4E\x70\x70\x7E\x7E" \
  211.    "\x30\x3E\x3F\x37\x3B\x78\x70\x7E\x7E\x03" \
  212.    "\x4F\x7E\x5E\x55\x0B\x3E\x03\x4F\x0E\x07" \
  213.    "\x0F\x00\x00"
  214.  
  215. // Black and white palette
  216. #define cpB \
  217.    "\x70\x70\x78\x7F\x07\x07\x0F\x07\x0F\x07" \
  218.    "\x70\x70\x07\x70\x00\x07\x0F\x07\x70\x70" \
  219.    "\x07\x70\x00\x70\x7F\x7F\x70\x07\x70\x07" \
  220.    "\x00\x70\x7F\x7F\x70\x07\x70\x70\x7F\x7F" \
  221.    "\x07\x0F\x0F\x78\x0F\x78\x07\x0F\x0F\x0F" \
  222.    "\x70\x0F\x07\x70\x70\x70\x07\x70\x0F\x07" \
  223.    "\x07\x00\x00"
  224.  
  225. // Monochrome palette
  226. #define cpM \
  227.    "\x70\x07\x07\x0F\x70\x70\x70\x07\x0F\x07" \
  228.    "\x70\x70\x07\x70\x00\x07\x0F\x07\x70\x70" \
  229.    "\x07\x70\x00\x70\x70\x70\x07\x07\x70\x07" \
  230.    "\x00\x70\x70\x70\x07\x07\x70\x70\x70\x0F" \
  231.    "\x07\x07\x0F\x70\x0F\x70\x07\x0F\x0F\x07" \
  232.    "\x70\x07\x07\x70\x07\x07\x07\x70\x0F\x07" \
  233.    "\x07\x00\x00"
  234.  
  235.  
  236. // Option flags for toggles dialog
  237. const ofSystem = 1;
  238. const ofSounds = 2;
  239.  
  240.  
  241. class THandlerDemo : public TApplication
  242. {
  243.    public:
  244.       THandlerDemo();
  245.       virtual void shutDown();
  246.       virtual TPalette& getPalette() const;
  247.    private:
  248.       void handleEvent(TEvent& Event);
  249.       static TDeskTop *initDeskTop(TRect r);
  250.       static TMenuBar *initMenuBar(TRect r);
  251.       static TStatusLine *initStatusLine(TRect r);
  252.       ushort toggleData;          // Toggles dialog data
  253.       Boolean systemCalls;        // True = use system calls
  254.       THandler *newHandler;       // Pointer to error handler object
  255.       ushort testDevices();       // Device test dialog
  256.       void toggleDialog();        // Toggles dialog
  257.       void aboutDialog();         // About dialog
  258.       void toggleVideo();         // Toggle video mode
  259. };
  260.  
  261.  
  262. THandlerDemo::THandlerDemo() :
  263.               TProgInit(initStatusLine, initMenuBar, initDeskTop)
  264. {
  265.    // Create the THandler object with sounds on
  266.    newHandler = new THandler(True);
  267.  
  268.    // Initialize toggles dialog data
  269.    toggleData = ofSounds;
  270.    systemCalls = False;
  271.  
  272.    // Draw the desktop
  273.    setState(sfExposed, True);
  274.    redraw();
  275.  
  276.    // Show "About" box
  277.    TEvent event;
  278.    event.what = evCommand;
  279.    event.message.command = cmAbout;
  280.    putEvent(event);
  281. }
  282.  
  283.  
  284. void THandlerDemo::shutDown()
  285. {
  286.    if(newHandler != 0)
  287.       delete newHandler;
  288.  
  289.    newHandler = 0;
  290.  
  291.    TProgram::shutDown();
  292. }
  293.  
  294.  
  295. TDeskTop *THandlerDemo::initDeskTop(TRect r)
  296. {
  297.    r.a.y++;
  298.    r.b.y--;
  299.    TProgram::application->setState(sfExposed, False);
  300.    return new TDeskTop(r);
  301. }
  302.  
  303.  
  304. TPalette& THandlerDemo::getPalette() const
  305. {
  306.    static TPalette c(cpC, sizeof(cpC) - 1);
  307.    static TPalette b(cpB, sizeof(cpB) - 1);
  308.    static TPalette m(cpM, sizeof(cpM) - 1);
  309.    static TPalette *palettes[] = {&c, &b, &m};
  310.    return *(palettes[appPalette]);
  311. }
  312.  
  313.  
  314. void THandlerDemo::handleEvent(TEvent& event)
  315. {
  316.    TApplication::handleEvent(event);
  317.  
  318.    if(event.what == evCommand)
  319.       {
  320.       switch(event.message.command)
  321.          {
  322.          // Call testDevices until user selects Cancel
  323.          case cmTests:
  324.             while(testDevices() != cmCancel);
  325.             break;
  326.          case cmToggle:
  327.             toggleDialog();
  328.             break;
  329.          case cmAbout:
  330.             aboutDialog();
  331.             break;
  332.          case cmMode:
  333.             toggleVideo();
  334.             break;
  335.          default:
  336.             return;
  337.          }
  338.       clearEvent(event);
  339.       }
  340. }
  341.  
  342.